package com.lyh.controller;

import com.lyh.pojo.Result;
import com.lyh.pojo.Ticket;
import com.lyh.pojo.User;
import com.lyh.service.TicketService;
import com.lyh.utils.TackleDataUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lyh.utils.HtmlCode;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/purchase")
@Controller
public class PurchaseController {

    @Autowired
    TicketService ticketService;

    /**
     * 购买票
     * 生成票的样式, 锁单, 并非真正的购买
     */
    @RequestMapping("/ticket/{no}")
    String purchaseTicket(@PathVariable("no") String no, Model model){

        Subject subject = SecurityUtils.getSubject();
        //获取登陆的账号
        User currentUser = (User) subject.getPrincipal();

        //校验该登陆的账号, 是否已邮箱激活
        if(subject.hasRole("active")){

            Result<String> result = ticketService.purchaseTicket(no,currentUser.getUserId()+"");

            if(result.isFlag()){
                //购买成功
                Ticket ticket = (Ticket) result.getData();
                //变为中文
                ticket.setSeatType(TackleDataUtils.translateSeatType(ticket.getSeatType()));
                model.addAttribute(ticket);
                return "purchase";
            }else {
                //购买失败, 根据错误类型, 返回通知信息
                String msg = null;
                if(result.getErrorType()==2)
                    msg = "抱歉, 您要购买的车票已售空, 请尝试购买其他票!";
                else
                    msg = "抱歉, 您还有未支付的订单, 请支付后再尝试购票!";
                HtmlCode.setMsg(model, msg,0,"index");
                return "msg";
            }
        }else {
            HtmlCode.setMsg(model,"抱歉, 您的账号尚未激活, 请激活后再尝试购票!",0,"index");
            return "msg";
        }
    }

    @RequestMapping("/pay/{orderId}")
    public String pay(@PathVariable("orderId") String orderId,Model model){
        Result<String> result = ticketService.pay(orderId);
        if(result.isFlag()){
            HtmlCode.setMsg(model,"购买成功! 详情可查看个人订单处",1,"index");
        }else {
            HtmlCode.setMsg(model,"该订单已支付或不存在!",0,"index");
        }
        return "msg";
    }

    /**
     * 查询该用户的票数据
     * @param type 查询的票类型
     * @return JSON形式的票数据
     */
    @RequestMapping("/displayOrder/{type}")
    @ResponseBody
    public Result<Ticket> displayOrder(@PathVariable("type")String type){
        Subject subject = SecurityUtils.getSubject();
        User currentUser = (User) subject.getPrincipal();
        Integer userId = currentUser.getUserId();
        Result<Ticket> result = null;
        int paidStatus = -1;
        switch (type){
            case "unpaid": paidStatus = 0;
                break;
            case "paid": paidStatus = 1;
                break;
            case "all": paidStatus = 2;
                break;
            case "refund": paidStatus = 3;
                break;
            case "finish": paidStatus = 4;
                break;
            default:
                result = new Result<Ticket>();
                result.setFlag(false);
                result.setMsg("请输入正确的查询状态！");
                return result;
        }
        result = ticketService.queryTicket(userId,paidStatus);

        return result;
    }



}
